<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends \Common\Controller\AdminController{
    public function index()
    {
        $arr=array();

        $date=date('Y-m-d',time());
        $date=strtotime($date);
        $D=M('user');
        $udata=$D->field('count(1) as total')->select();
        $udata1=$D->field('count(1) as total')->where('status=1')->select();//已开通用户
        $udata2=$D->field('count(1) as total')->where('status=0')->select();//未开通用户
        $udata3=$D->field('count(1) as total')->where('createtime>'.$date)->select();//今日新增用户
        $arr['usertotal']=$udata[0]['total'];
        $arr['usertotal1']=$udata1[0]['total'];
        $arr['usertotal2']=$udata2[0]['total'];
        $arr['usertotal3']=$udata3[0]['total'];
        $I=M('income');
        $data=$I->field('sum(price) as total')->where('status=1 and gotegary=2')->select();
        $arr['income']=$data[0]['total'];
        $T=M('tohelp');
        $G=M('gethelp');
        $tdata=$T->field('sum(price) as total')->select();
        $gdata=$G->field('sum(price) as total')->select();
        $arr['tohelp']=$tdata[0]['total'];
        $arr['gethelp']=$gdata[0]['total'];

        $tdata1=$T->field('sum(price) as total')->where('status=0')->select();
        $gdata1=$G->field('sum(price) as total')->where('status=0')->select();
        $arr['tohelp1']=$tdata1[0]['total'];
        $arr['gethelp1']=$gdata1[0]['total'];

        $this->assign('room',$arr);

        // $this->islogin();
        $this->display();
    }

    //添加会员
    public function add()
    {
        $this->display();
    }

    //保存会员
    public function doadd()
    {
        $username=I('post.username');
        $password=md5(I('post.password'));
        $paypassword=md5(I('post.paypassword'));

        $password=$this->md5pw($password);
        $paypassword=$this->md5pw($paypassword);

        $tusername=I('post.tusername');
        $paper=I('post.paper');
        $weixin=I('post.weixin');
        $alipay=I('post.alipay');
        $mobile=I('post.mobile');
        $bank=I('post.bank');
        $bankname=I('post.bankname');
        $bankcode=I('post.bankcode');
        $createtime=time();

        $D=M('user');
        $arr=array(
            'username'=>$username,
            'password'=>$password,
            'paypassword'=>$paypassword,
            'tusername'=>$tusername,
            'paper'=>$paper,
            'weixin'=>$weixin,
            'alipay'=>$alipay,
            'mobile'=>$mobile,
            'bank'=>$bank,
            'bankname'=>$bankname,
            'bankcode'=>$bankcode,
            'createtime'=>$createtime,
            'status'=>1,
            'number'=>'',
            'gotegary'=>0,
        );
        $id=$D->add($arr);

        $data=$D->where('username="'.$tusername.'"')->find();
        if(empty($data))
        {
            $path='0,'.$id.',';
        }
        else
        {
            $path=$data['path'].$id.',';
        }

        $number=$this->donumber($id);
        $arr=array(
            'number'=>$number,
            'path'=>$path,
        );
        $D->where('id='.$id)->save($arr);
        echo"<script>alert('会员注册成功！');history.go(-1);</script>";
        die();

    }
    //生成会员编号
    private function donumber($id)
    {

        $number='A'.str_pad($id,9,"0",STR_PAD_LEFT);
        return $number;
    }

    //添加管理勋章
    public function adminadd()
    {
        $D=M('power');
        $data=$D->select();
        $arr=array();

        foreach($data as $key=>$vo)
        {
            if($vo['parentid']==0)
            {
                $arr[$key]=$vo;
            }
        }
       for($i=0;$i<count($arr);$i++)
       {
           $arr1=array();
           $j=0;//数据从零开始
           foreach($data as $key=>$vo) {


               if($vo['parentid']==$arr[$i]['id']&&$vo['parentid']!=0) {


                   $arr1[$j]=$vo;
                   $j++;


               }
           }


           $arr[$i]['sub']=$arr1;

       }


      $this->assign('room',$arr);



        $this->display();
    }


    //添加管理勋章
    public function adminedit()
    {

       
        $id=I('get.id');
        $A=M('admin');
        $adata=$A->where('id='.$id)->find();

        
        
        $str=$adata['power'];


        $D=M('power');
        $data=$D->order('id asc')->select();
        $arr=array();


        for($i=0;$i<count($data);$i++)
        {
            $sub=substr($str,$i,1);
            $data[$i]['power']=$sub;
        }

        foreach($data as $key=>$vo)
        {
            if($vo['parentid']==0)
            {
                $arr[$key]=$vo;
            }
        }
        for($i=0;$i<count($arr);$i++)
        {
            $arr1=array();
            $j=0;//数据从零开始
            foreach($data as $key=>$vo) {


                if($vo['parentid']==$arr[$i]['id']&&$vo['parentid']!=0) {


                    $arr1[$j]=$vo;
                    $j++;


                }
            }


            $arr[$i]['sub']=$arr1;

        }


        $this->assign('room',$arr);



        $this->assign('adata',$adata);

        $this->display();
    }

    //生成管理员
    public function doadminadd()
    {
        $power=I('post.power');
        $username=I('post.username');
        $password=I('post.password');
        $password=$this->md5pw($password);


        $realname=I('post.realname');
        $job=I('post.job');
        $U=M('admin');
        $udata=$U->field('id')->where('adminname="'.$username.'"')->find();
        if(!empty($udata))
        {
            echo"<script>alert('用户已经存在，请重新输入用户帐户！');history.go(-1);</script>";
            die();
        }


        $D=M('power');
        $data=$D->field('count(1) as total')->select();
        $total=$data[0]['total'];
        $str='';
        for($i=0;$i<$total;$i++)
        {
           $str.='0';
        }
        foreach($power as $vo)
        {
             $str=substr_replace($str,'1',$vo-1,1);
        }
        $arr=array(
            'adminname'=>$username,
            'password'=>$password,
            'realname'=>$realname,
            'job'=>$job,
            'power'=>$str,
            'createtime'=>time()
        );

    
        $U->add($arr);
        echo"<script>alert('添加管理员成功。');location.href='/admin/index/admin.html';</script>";
        die();


    }
    //修改管理员
    public function doadminedit()
    {
        $id=I('post.id');
        $power=I('post.power');
        $username=I('post.username');

        $password=I('post.password');

        $realname=I('post.realname');
        $job=I('post.job');
        $U=M('admin');
        $udata=$U->field('id,password')->where('id='.$id)->find();
        if(!empty($password))
        {
            $password=$this->md5pw($password);
        }
        else
        {
            $password=$udata['password'];
        }


        $D=M('power');
        $data=$D->field('count(1) as total')->select();
        $total=$data[0]['total'];
        $str='';
        for($i=0;$i<$total;$i++)
        {
            $str.='0';
        }
        foreach($power as $vo)
        {
            $str=substr_replace($str,'1',$vo-1,1);
        }
        $arr=array(
            'adminname'=>$username,
            'password'=>$password,
            'realname'=>$realname,
            'job'=>$job,
            'power'=>$str,
        );
        $U->where('id='.$id)->save($arr);
        echo"<script>alert('修改管理员成功。');location.href='/admin/index/admin.html';</script>";
        die();


    }

    //管理员列表
    public function admin()
    {

        $keyword=I('get.k');

        $begintime=I('get.b');
        $endtime=I('get.e');
        $search='';


        if(!empty($keyword))
        {
            $search='&k='.$keyword;
        }

        if(!empty($begintime))
        {

            $search='&b='.$begintime;
            $begintime=strtotime($begintime);
        }
        if(!empty($endtime))
        {

            $search.='&e='.$endtime;
            $endtime=strtotime($endtime);
        }





        $sqlw='1=1 ';

        if($begintime!='' and $endtime!='')
        {
            $sqlw.=' and createtime>='.$begintime.' and createtime<'.($endtime+24*60*60);
        }
        elseif($begintime!='' and $endtime=='')
        {
            $sqlw.=' and createtime>='.$begintime.' and createtime<'.($begintime+24*60*60);
        }
        elseif($begintime=='' and $endtime!='')
        {
            $sqlw.=' and createtime>='.$endtime.' and createtime<'.($endtime+24*60*60);
        }



        $page = empty($_REQUEST['page'])?1:$_REQUEST['page'];
        $pagesize =10;




        $sqlw.=' and (adminname like "%'.$keyword.'%" )';

        $room = M('admin');





        $info=$room->order(" id desc")->where($sqlw)->limit(($page-1)*$pagesize,$pagesize)->select();

        $sql = "select count(*) as total from tm_admin where ".$sqlw;


        $query = $room->query($sql);





        $pageurl="/admin/index/admin?";


        $count = $query[0]['total'];

        $this->assign("room",$info);


        $this->assign("total",$count);

        if($count%$pagesize==0)
            $this->assign("count",$count/$pagesize);
        else
            $this->assign("count",(int)floor($count/$pagesize)+1);

        $this->assign("page",$page);


        $this->assign("search",$search);

        $this->assign("pageurl",$pageurl);
        $this->assign('pagesize',$pagesize);

        $this->assign('keyword',$keyword);
        $this->assign('begintime',$begintime);
        $this->assign('endtime',$endtime);


        $this->display();
    }
    //修改密码
    public function password()
    {
        $this->display();
    }
    //处理修改密码
    public function dopassword()
    {
        $password=I('post.password');
        $newpassword=I('post.newpassword');

        $newpassword=$this->md5pw($newpassword);
        $userid=session('adminid');
        $D=M('admin');
        $data=$D->where('id='.$userid)->find();

        if(!$this->_checkPwd($password,$data['password']))
        {
            echo 2;
            die();

        }
        else
        {
            $arr=array(
                'password'=>$newpassword,
            );
            $D->where('id='.$userid)->save($arr);
            echo 1;
            die();

        }
        $this->display();
    }

    //修改密码
    public function user_password()
    {
        $id=I('get.id');
        $this->assign('id',$id);
        $this->display();
    }
    public function setting()
    {
        $D=M('setting');

        $data=$D->where('id=1')->find();
        $this->assign('setting',$data);
        $this->display();
    }
    public function dosetting()
    {
        $D=M('setting');
        $web_config=I('post.web_config');
        $tohelp=I('post.tohelp');
        $gethelp=I('post.gethelp');
        $arr=array('web_config'=>$web_config,
            'tohelp'=>$tohelp,
            'gethelp'=>$gethelp,
            );
        $D->where('id=1')->save($arr);
        echo 1;
        die();
    }

    //参数
    public function parameter()
    {
        $D=M('parameter');
        $data=$D->order('id asc')->select();
        $this->assign('room',$data);
        $this->display();
    }
    //处理参数
    public function doparameter()
    {
        $D=M('parameter');
        $arr=array();
        for($i=0;$i<=18;$i++)
        {
            $arr[$i]=I('post.number'.$i);
        }

        foreach($arr as $key=>$vo)
        {

            $arr1=array(
                'number'=>$vo
            );
            $i=$key+1;
            $D->where('id='.$i)->save($arr1);
        }


        $pdata=$D->order('id asc')->select();
        F('para',$pdata);




        echo "<script>alert('修改奖金参数成功！');history.go(-1);</script>";
        die();

        //$data=$D->select();
        //$this->assign('room',$data);
    }
    //注册新会员
    public function douseradd()
    {


        $username = I('post.username');
        $password = md5(I('post.password'));
        $paypassword = md5(I('post.paypassword'));

        $password = $this->md5pw($password);
        $paypassword = $this->md5pw($paypassword);

        $tusername = I('post.tusername');
        $realname = I('post.realname');
        $mobile = I('post.mobile');

        $paper = I('post.paper');
        $bankname = I('post.bankname');
        $bank = I('post.bank');
        $bankcode = I('post.bankcode');
        $weixin = I('post.weixin');
        $alipay = I('post.alipay');
        $address = I('post.address');


        //$userid = session('userid');
        $createtime = time();
        


            //判断支付密码是否正确
        $D = M('user');
        //$data = $D->where('id=' . $userid)->find();
        //取得推荐人ＩＤ
        $data1 = $D->where('username="' . $username . '"')->find();
        if (!empty($data1)) {
            echo 2;//用户名已经存在，
            die();
        }
        if(!empty($tusername))
        {
            $pdata = $D->where('username="' . $tusername . '"')->find();
            if (empty($pdata)) {
                echo 3;//推荐人不存在，
                die();
            }
        }
        //判断手机号码
        $data2 = $D->where('mobile="' . $mobile . '"')->find();
        if (!empty($data2)) {
            echo 4;//手机号码已经存在，
            die();
        }
        //判断手机号码
        $data3 = $D->where('paper="' . $paper . '"')->find();
        if (!empty($data3)) {
            echo 6;//手机号码已经存在，
            die();
        }




        $arr=array(
            'username'=>$username,
            'password'=>$password,
            'paypassword'=>$paypassword,
            'tusername'=>$tusername,
            'realname'=>$realname,
            'mobile'=>$mobile,
            'createtime'=>$createtime,
            'gotegary'=>0,
            'status'=>1,
            'paper'=>$paper,
            'bankname'=>$bankname,
            'bank'=>$bank,
            'bankcode'=>$bankcode,
            'weixin'=>$weixin,
            'alipay'=>$alipay,
            'address'=>$address,


        );



        $id=$D->add($arr);//添加会员
        $path=$pdata['path'].$id.',';
        $number=$this->donumber($id);
        $arr=array('path'=>$path,
            'number'=>$number,
        );
        $D->where('id='.$id)->save($arr);


        $I=M('integral');
        $arri=array(
            'userid'=>$id,
            'integral1'=>0,
            'integral2'=>0,
            'integral3'=>0,
            'integral4'=>0,
            'integral5'=>0,
        );
        $I->add($arri);//加入积分表
        echo 1;
        die();


    }

    //我的团队
    public function member()
    {
        $t=I('get.t');

        $keyword=trim(I('get.k'));

        $begintime=I('get.b');
        $endtime=I('get.e');
        $search='';


        if(!empty($t))
        {
            $search='&t='.$t;
        }
        else
        {
            $search='&t=1';
        }

        if(!empty($keyword))
        {
            $search='&k='.$keyword;
        }

        if(!empty($begintime))
        {

            $search='&b='.$begintime;
            $begintime=strtotime($begintime);
        }
        if(!empty($endtime))
        {

            $search.='&e='.$endtime;
            $endtime=strtotime($endtime);
        }




        if(empty($t))
        {
             $sqlw='1=1 and status>=1';
        }
        else
        {
            if($t==1)
               $sqlw='1=1 and status>=1';
            elseif($t==2)
               $sqlw='1=1 and status=0';
            elseif($t==3)
                $sqlw='1=1 and tohelp=0';
        }

        if($begintime!='' and $endtime!='')
        {
            $sqlw.=' and createtime>='.$begintime.' and createtime<'.($endtime+24*60*60);
        }
        elseif($begintime!='' and $endtime=='')
        {
            $sqlw.=' and createtime>='.$begintime.' and createtime<'.($begintime+24*60*60);
        }
        elseif($begintime=='' and $endtime!='')
        {
            $sqlw.=' and createtime>='.$endtime.' and createtime<'.($endtime+24*60*60);
        }



        $page = empty($_REQUEST['page'])?1:$_REQUEST['page'];
        $pagesize =10;




        $sqlw.=' and (number like "%'.$keyword.'%" or username like "%'.$keyword.'%" or mobile like "%'.$keyword.'%" or realname like "%'.$keyword.'%" )';

        $room = M('user');





        $info=$room->order(" id desc")->where($sqlw)->limit(($page-1)*$pagesize,$pagesize)->select();

        $sql = "select count(*) as total from tm_user where ".$sqlw;


        $query = $room->query($sql);





        $pageurl="/admin/index/member.html?";


        $count = $query[0]['total'];

        $this->assign("room",$info);


        $this->assign("total",$count);

        if($count%$pagesize==0)
            $this->assign("count",$count/$pagesize);
        else
            $this->assign("count",(int)floor($count/$pagesize)+1);

        $this->assign("page",$page);


        $this->assign("search",$search);

        $this->assign("pageurl",$pageurl);
        $this->assign('pagesize',$pagesize);

        $this->assign('keyword',$keyword);
        $this->assign('begintime',$begintime);
        $this->assign('endtime',$endtime);


        $this->display();
    }

    public function status()
    {
        $id=I('post.id');
        $status=I('post.status');
        $D=M('user');
        $arr=array('status'=>$status);
        $D->where('id='.$id)->save($arr);
        //得到帮助返回匹配例表
        if($status==3)
        {
            $TO=M('tohelp');
            $G=M('gethelp');
            $tdata=$TO->where('userid='.$id.' and status=1')->find();
            if(!empty($tdata))
            {
                $T=M('toget');
                $data=$T->where('toid='.$tdata['id'].' and status=1')->select();
                if(!empty($data))
                {

                    foreach($data as $vo)
                    {
                        $arr=array('status'=>0);
                        $G->where('id='.$vo['getid'])->save($arr);
                    }
                    $T->where('toid='.$tdata['id'].' and status=1')->delete();
                }
                $TO->where('userid='.$id.' and status=1')->delete();
            }
        }
        echo 1;
        die();


    }

    public function tohelp()
    {
        $id=I('post.id');
        $status=I('post.status');
        $D=M('user');
        $arr=array('tohelp'=>$status);
        $D->where('id='.$id)->save($arr);
        $user=$D->field('mobile,username')->where('id='.$id)->find();

        //充许排单
        if($status==1)
			//$this->sms($user['mobile'],$user['username'], 6);
        echo 1;
        die();


    }
    //推荐图谱
    public function team(){
        $str = '';
        $username = session('username');

        $D = M('user');
        $data = $D->where('tusername="0"')->select();

        $str .= '<ul id="tree" class="treeview">';
        foreach ($data as $vo) {

            $str .= '<li><span>'.$vo['username'].'</span>';
            $str .= $this->getsubuser($vo['username']);
            $str .= '</li>';
        }
         $str .= '</ul>';
        //$str.=$this->getsubuser($username);

        $this->assign('str',$str);
        $this->display();
    }
    //得到子项
    public function getsubuser($username)
    {
        $D=M('user');

        $room=$D->field('username,tusername')->where('tusername="'.$username.'"')->select();


        $str='';
        if(!empty($room))
        {
            $str.='<ul>';
            foreach($room as $vo)
            {
                //$arr['username']=
                $str.='<li><span>'.$vo['username'].'</span>';
                $str.=$this->getsubuser($vo['username']);
                $str.='</li>';


            }
            $str.='</ul>';
        }


        return $str;



    }


    //购买排单币
    public function paidan()
    {

        $this->display();
    }
    //处理购买排单币　
    public function dopaidan()
    {


        $username=I('post.username');
        $price=I('post.price');
        $gotegary=I('post.gotegary');
        $D=M('user');
        $data=$D->where('username="'.$username.'"')->find();
        if(empty($data))
        {
            echo 2;
            die();
        }

        $I=M('integral');
        $idata=$I->where('userid='. $data['id'])->find();

        if($gotegary==1)
        {
            $arr=array(
                'integral3'=>$idata['integral3']+$price,
            );
            $I->where('userid='.$data['id'])->save($arr);
            //支入记录
            $title=$username.'充值'.$price.'排单币';
            $createtime=time();
            $this->Pay_history($data['id'],$title,$price,$createtime,1,3);
            //充值记录
            $this->case_history($data['id'],$price,1,1,$createtime,$title);
        }
        else {
            if ($idata['integral3'] < $price)
            {
                echo 3;
                die();
            }
            else
            {
                $arr=array(
                    'integral3'=>$idata['integral3']-$price,
                );
                $I->where('userid='.$data['id'])->save($arr);
                //支入记录
                $title=$username.'扣除'.$price.'排单币';
                $createtime=time();
                $this->Pay_history($data['id'],$title,$price,$createtime,0,3);
                //充值记录
                $this->case_history($data['id'],$price,4,1,$createtime,$title);
            }
        }
        echo 1;
        die();
    }
    //排单币对帐
    public function lpaidan()
    {



        $begintime=I('get.b');
        $endtime=I('get.e');
        $search='';



        if(!empty($begintime))
        {

            $search='&b='.$begintime;
            $begintime=strtotime($begintime);
        }
        if(!empty($endtime))
        {

            $search.='&e='.$endtime;
            $endtime=strtotime($endtime);
        }





        $sqlw='1=1 ';

        if($begintime!='' and $endtime!='')
        {
            $sqlw.=' and createtime>='.$begintime.' and createtime<'.($endtime+24*60*60);
        }
        elseif($begintime!='' and $endtime=='')
        {
            $sqlw.=' and createtime>='.$begintime.' and createtime<'.($begintime+24*60*60);
        }
        elseif($begintime=='' and $endtime!='')
        {
            $sqlw.=' and createtime>='.$endtime.' and createtime<'.($endtime+24*60*60);
        }



        $page = empty($_REQUEST['page'])?1:$_REQUEST['page'];
        $pagesize =10;
        $userid=session('userid');




        $sqlw.=' and gotegary=1';

        $room = M('buy_history');





        $info=$room->order(" id desc")->where($sqlw)->limit(($page-1)*$pagesize,$pagesize)->select();

        $sql = "select count(*) as total from tm_buy_history where ".$sqlw;


        $query = $room->query($sql);





        $pageurl="/admin/index/lpaidan?";


        $count = $query[0]['total'];

        $this->assign("room",$info);


        $this->assign("total",$count);

        if($count%$pagesize==0)
            $this->assign("count",$count/$pagesize);
        else
            $this->assign("count",(int)floor($count/$pagesize)+1);

        $this->assign("page",$page);


        $this->assign("search",$search);

        $this->assign("pageurl",$pageurl);
        $this->assign('pagesize',$pagesize);


        $this->assign('begintime',$begintime);
        $this->assign('endtime',$endtime);


        $this->display();
    }
    //购买激活码
    public function jihuoma()
    {

        $this->display();
    }
    //处理购买激活码
    public function dojihuoma()
    {
        $username = I('post.username');
        $total = I('post.total');
        $price = I('post.price');

        $gotegary = I('post.gotegary');

        $D = M('user');
        $data = $D->where('username="' . $username . '"')->find();
        if(empty($data))
        {
            echo 2;
            die();
        }


        $I = M('integral');
        $idata = $I->where('userid=' . $data['id'])->find();
        $userid = $data['id'];

        if ($gotegary == 1) {
            $arr = array(

                'integral4' => $idata['integral4'] + $price,
            );
            $I->where('userid=' . $userid)->save($arr);
            //支入记录
            $title = $username . '充值' . $price . '激活码';
            $createtime = time();
            $this->Pay_history($userid, $title, $total, $createtime, 1, 4);
            //激活码记录
            $this->case_history($userid, $price, 1, 2, $createtime, $title);
       }
        else
        {

            if($idata['integral4']<$price)
            {
                echo 3;
                die();
            }
            $arr = array(

                'integral4' => $idata['integral4'] - $price,
            );
            $I->where('userid=' . $userid)->save($arr);
            //支入记录
            $title = $username . '扣除' . $price . '激活码';
            $createtime = time();
            $this->Pay_history($userid, $title, $total, $createtime, 0, 4);
            //激活码记录
            $this->case_history($userid, $price, 4, 2, $createtime, $title);
        }
        echo 1;
        die();
    }
//激活码对帐
    public function ljihuoma()
    {



        $begintime=I('get.b');
        $endtime=I('get.e');
        $search='';



        if(!empty($begintime))
        {

            $search='&b='.$begintime;
            $begintime=strtotime($begintime);
        }
        if(!empty($endtime))
        {

            $search.='&e='.$endtime;
            $endtime=strtotime($endtime);
        }





        $sqlw='1=1 ';

        if($begintime!='' and $endtime!='')
        {
            $sqlw.=' and createtime>='.$begintime.' and createtime<'.($endtime+24*60*60);
        }
        elseif($begintime!='' and $endtime=='')
        {
            $sqlw.=' and createtime>='.$begintime.' and createtime<'.($begintime+24*60*60);
        }
        elseif($begintime=='' and $endtime!='')
        {
            $sqlw.=' and createtime>='.$endtime.' and createtime<'.($endtime+24*60*60);
        }



        $page = empty($_REQUEST['page'])?1:$_REQUEST['page'];
        $pagesize =10;
        $userid=session('userid');




        $sqlw.=' and gotegary=2';

        $room = M('buy_history');





        $info=$room->order(" id desc")->where($sqlw)->limit(($page-1)*$pagesize,$pagesize)->select();

        $sql = "select count(*) as total from tm_buy_history where ".$sqlw;


        $query = $room->query($sql);





        $pageurl="/admin/index/ljihuoma?";


        $count = $query[0]['total'];

        $this->assign("room",$info);


        $this->assign("total",$count);

        if($count%$pagesize==0)
            $this->assign("count",$count/$pagesize);
        else
            $this->assign("count",(int)floor($count/$pagesize)+1);

        $this->assign("page",$page);


        $this->assign("search",$search);

        $this->assign("pageurl",$pageurl);
        $this->assign('pagesize',$pagesize);


        $this->assign('begintime',$begintime);
        $this->assign('endtime',$endtime);


        $this->display();
    }


    //支付记录
    //$userid 会员ID
    //$title 内容
    //$createtime 创建时间
    //$price 消费价格
    //$status 消费状态 1 收入，0 支出
    //$gotegary 1  静态帐户，2动态帐户，3 排单币，4激活币 5  消费帐户



    private function Pay_history($userid,$title,$price,$createtime,$status,$gotegary)
    {
        $D=M('income');
        $arr=array(
            'userid'=>$userid,
            'title'=>$title,
            'price'=>$price,
            'createtime'=>$createtime,
            'status'=>$status,
            'gotegary'=>$gotegary,

        );
        $D->add($arr);
    }

    //购买记录
    //userid 用户名
    //price 花费价格
    //title 内容
    //status １购买　２转帐　３使用 4扣除
    //gotegary 1  排单币 2 激活码 3 静态用户　4动态用户


    public function case_history($userid,$price,$status,$gotegary,$createtime,$title)
    {
        $D=M('buy_history');
        $arr=array('userid'=>$userid,
            'price'=>$price,
            'status'=>$status,
            'gotegary'=>$gotegary,
            'createtime'=>$createtime,
            'title'=>$title
        );


        $D->add($arr);

    }



    public function pipei()
    {
        $T=M('tohelp');
        $G=M('gethelp');
        //１控制时间；
        $tdata=$T->field('id,userid,orderid,price,begintime,status,createtime,surplus,(select username from tm_user where id=tm_tohelp.userid) as username,(select realname from tm_user where id=tm_tohelp.userid) as realname,(select count(1) from tm_toget where toid=tm_tohelp.id) as number')->where('status=0')->limit(5)->select();

        for($i=0;$i<count($tdata);$i++)
        {

            //剩余金额的
            if($tdata[$i]['surplus']>0&&!empty($tdata[$i]['surplus']))
            {

                $pricet=$tdata[$i]['surplus'];
            }
            else
            {
                $pricet=$tdata[$i]['price'];
            }


            //相同金额的
            $gd=$G->field('id,orderid,price,begintime,status,createtime,surplus,(select username from tm_user where id=tm_gethelp.userid) as username,(select realname from tm_user where id=tm_gethelp.userid) as realname')->where('price='.$pricet.' and status=0 and userid!='.$tdata[$i]['userid'])->limit(1)->select();

            if(empty($gd))
            {

                //不同金额的
                $gd=$G->field('id,orderid,price,begintime,status,createtime,surplus,(select username from tm_user where id=tm_gethelp.userid) as username,(select realname from tm_user where id=tm_gethelp.userid) as realname')->where('price<'.$pricet.' and status=0 and userid!='.$tdata[$i]['userid'])->select();

                $price=0;

                foreach($gd as $vo)
                {
                    $price= $price + $vo['price'];


                    if($price > $tdata[$i]['price'])
                    {

                        break;
                    }

                }


            }
            else
            {

                $tdata[$i]['sub']=$gd;

            }
        }
      

        $gdata=$G->field('id,orderid,userid,price,begintime,status,createtime,surplus,(select username from tm_user where id=tm_gethelp.userid) as username,(select realname from tm_user where id=tm_gethelp.userid) as realname,(select count(1) from tm_toget where getid=tm_gethelp.id) as number')->where('status=0')->limit(5)->select();
        for($i=0;$i<count($gdata);$i++)
        {
            //剩余金额的
            if($gdata[$i]['surplus']>0&&!empty($gdata[$i]['surplus']))
            {
                $priceg=$gdata[$i]['surplus'];
            }
            else
            {
                $priceg=$gdata[$i]['price'];
            }
            //相同金额的
            $gd1=$T->field('id,orderid,price,begintime,status,createtime,surplus,(select username from tm_user where id=tm_tohelp.userid) as username,(select realname from tm_user where id=tm_tohelp.userid) as realname')->where('price='.$priceg.' and status=0 and userid!='.$gdata[$i]['userid'])->limit(1)->select();

            if(empty($gd1))
            {

                //不同金额的
                $gd1=$T->field('id,orderid,price,begintime,status,createtime,surplus,(select username from tm_user where id=tm_tohelp.userid) as username,(select realname from tm_user where id=tm_tohelp.userid) as realname')->where('price<'.$priceg.' and status=0 and userid!='.$gdata[$i]['userid'])->select();

                $price=0;

                foreach($gd1 as $vo)
                {
                    $price= $price + $vo['price'];


                    if($price > $gdata[$i]['price'])
                    {

                        break;
                    }

                }


            }
            else
            {

                $gdata[$i]['sub']=$gd1;

            }
        }


        $this->assign('tdata',$tdata);
        $this->assign('gdata',$gdata);
        $this->display();
    }

    //处理匹配
    public function dopipei(){
        $action=I('post.action');
        switch($action)
        {
            case 1;
                $id=I('post.id');
                $str=I('post.str');
                $str=substr($str,0,strlen($str)-1);


                $arr=explode(",",$str);



                $D=M('toget');
                $createtime=time();
                $D->where('toid='.$id)->delete();


                $T=M('tohelp');
                $arrt=array('status'=>1,'begintime'=>$createtime);
                $T->where('id='.$id)->save($arrt);

                $G=M('gethelp');
                $arrg=array('status'=>1,'begintime'=>$createtime);



                //发送短信
                //提供帮助
                $udata=$T->field('(select username from tm_user where id=tm_tohelp.userid) as username,(select mobile from tm_user where id=tm_tohelp.userid) as mobile')->where('id='.$id)->find();
                $this->sms($udata['mobile'],$udata['username'],4);
                //得到帮助


                foreach($arr as $vo)
                {
                    if(empty($vo))
                    {
                        break;
                    }
                    $G->where('id='.$vo)->save($arrg);
                    $arr1=array(
                        'toid'=>$id,
                        'getid'=>$vo,
                        'createtime'=>$createtime,
                        'status'=>1,
                    );
                    $D->add($arr1);

                    //发送短信
                    $gdata=$G->field('(select username from tm_user where id=tm_gethelp.userid) as username,(select mobile from tm_user where id=tm_gethelp.userid) as mobile')->where('id='.$vo)->find();
                    $this->sms($gdata['mobile'],$gdata['username'],5);
                }
                echo 1;
                break;
            case 2:
                $id=I('post.id');
                $str=I('post.str');
                $str=substr($str,0,strlen($str)-1);

                $arr=explode(",",$str);

                $D=M('toget');
                $createtime=time();
                $D->where('getid='.$id)->delete();
                $T=M('gethelp');

                //if($gdata[0]['total']==$data['price'])
               // {
                    $arrt=array('status'=>1,'begintime'=>$createtime);

               // }
               // else
               // {
                  //  $arrt=array('surplus'=>$data['price']-$gdata[0]['total']);
               // }
                $T->where('id='.$id)->save($arrt);

                $G=M('tohelp');
                $arrg=array('status'=>1,'begintime'=>$createtime);


                //发送短信
                //提供帮助
                //发送短信
                $udata=$T->field('(select username from tm_user where id=tm_gethelp.userid) as username,(select mobile from tm_user where id=tm_gethelp.userid) as mobile')->where('id='.$id)->find();

                $this->sms($udata['mobile'],$udata['username'],5);


                foreach($arr as $vo) {
                    if (empty($vo)) {
                        break;
                    }
                    $G->where('id=' . $vo)->save($arrg);
                    $arr1 = array(
                        'toid' =>  $vo,
                        'getid' =>$id,
                        'createtime' => $createtime,
                        'status' => 1,
                    );
                    $D->add($arr1);

                    //发送短信
                    $gdata=$G->field('(select username from tm_user where id=tm_tohelp.userid) as username,(select mobile from tm_user where id=tm_tohelp.userid) as mobile')->where('id='.$vo)->find();
                    $this->sms($gdata['mobile'],$gdata['username'],4);

                }





                echo 1;
                break;
        }

    }
   //充值
    public function cash()
    {
        $this->display();
    }
    //处理　
    public function docash()
    {


        $username=I('post.username');
        $price=I('post.price');
        $integral=I('post.integral');
        $gotegary=I('post.gotegary');

        $D=M('user');
        $data=$D->where('username="'.$username.'"')->find();
        if(empty($data))
        {
            echo 2;
            die();
        }

        $I=M('integral');
        $idata=$I->where('userid='. $data['id'])->find();

        if($gotegary==1)
        {
            if($integral==1)
            {
                $arr=array(
                    'integral1'=>$idata['integral1']+$price,
                );
            }
            elseif($integral==2)
            {
                $arr=array(
                    'integral2'=>$idata['integral2']+$price,
                );
            }
            $I->where('userid='.$data['id'])->save($arr);
            //支入记录
            $createtime=time();
            if($integral==1) {
                $title = $username.'充值静态帐户'.$price.'成功';
                $this->Pay_history($data['id'],$title,$price,$createtime,1,1);
                //充值记录
                $this->case_history($data['id'],$price,1,3,$createtime,$title);
            }
            elseif($integral==2){
                $title = $username.'充值动态帐户'.$price.'成功';
                $this->Pay_history($data['id'],$title,$price,$createtime,1,2);
                //充值记录
                $this->case_history($data['id'],$price,1,4,$createtime,$title);
            }

        }
        else
        {




            if($integral==1)
            {

                if($idata['integral1']<$price)
                {
                    echo 3;
                    die();
                }
                $arr=array(
                    'integral1'=>$idata['integral1']-$price,
                );
            }
            elseif($integral==2)
            {
                if($idata['integral2']<$price)
                {
                    echo 4;
                    die();
                }
                $arr=array(
                    'integral2'=>$idata['integral2']-$price,
                );
            }
            $I->where('userid='.$data['id'])->save($arr);
            //支入记录
            $createtime=time();
            if($integral==1) {
                $title = $username.'扣除静态帐户'.$price.'成功';
                $this->Pay_history($data['id'],$title,$price,$createtime,0,1);
                //充值记录
                $this->case_history($data['id'],$price,4,3,$createtime,$title);
            }
            elseif($integral==2){
                $title = $username.'扣除动态帐户'.$price.'成功';
                $this->Pay_history($data['id'],$title,$price,$createtime,0,2);
                //充值记录
                $this->case_history($data['id'],$price,4,4,$createtime,$title);
            }
        }

        echo 1;
        die();
    }

    //静态与动态帐户充值对帐
    public function lcash()
    {



        $begintime=I('get.b');
        $endtime=I('get.e');
        $search='';



        if(!empty($begintime))
        {

            $search='&b='.$begintime;
            $begintime=strtotime($begintime);
        }
        if(!empty($endtime))
        {

            $search.='&e='.$endtime;
            $endtime=strtotime($endtime);
        }





        $sqlw='1=1 ';

        if($begintime!='' and $endtime!='')
        {
            $sqlw.=' and createtime>='.$begintime.' and createtime<'.($endtime+24*60*60);
        }
        elseif($begintime!='' and $endtime=='')
        {
            $sqlw.=' and createtime>='.$begintime.' and createtime<'.($begintime+24*60*60);
        }
        elseif($begintime=='' and $endtime!='')
        {
            $sqlw.=' and createtime>='.$endtime.' and createtime<'.($endtime+24*60*60);
        }



        $page = empty($_REQUEST['page'])?1:$_REQUEST['page'];
        $pagesize =10;
        $userid=session('userid');




        $sqlw.=' and (gotegary=3 or gotegary=4)';

        $room = M('buy_history');





        $info=$room->order(" id desc")->where($sqlw)->limit(($page-1)*$pagesize,$pagesize)->select();

        $sql = "select count(*) as total from tm_buy_history where ".$sqlw;


        $query = $room->query($sql);





        $pageurl="/admin/index/lcash?";


        $count = $query[0]['total'];

        $this->assign("room",$info);


        $this->assign("total",$count);

        if($count%$pagesize==0)
            $this->assign("count",$count/$pagesize);
        else
            $this->assign("count",(int)floor($count/$pagesize)+1);

        $this->assign("page",$page);


        $this->assign("search",$search);

        $this->assign("pageurl",$pageurl);
        $this->assign('pagesize',$pagesize);


        $this->assign('begintime',$begintime);
        $this->assign('endtime',$endtime);


        $this->display();
    }


    //匹配记录
    public function record()
    {

        $keyword=I('get.k');

        $begintime=I('get.b');
        $endtime=I('get.e');
        $search='';



        if(!empty($begintime))
        {

            $search='_b='.$begintime;
            $begintime=strtotime($begintime);
        }
        if(!empty($endtime))
        {

            $search.='_e='.$endtime;
            $endtime=strtotime($endtime);
        }





        $sqlw='1=1 ';

        if($begintime!='' and $endtime!='')
        {
            $sqlw.=' and tm_tohelp.createtime>='.$begintime.' and tm_tohelp.createtime<'.($endtime+24*60*60);
        }
        elseif($begintime!='' and $endtime=='')
        {
            $sqlw.=' and tm_tohelp.createtime>='.$begintime.' and tm_tohelp.createtime<'.($begintime+24*60*60);
        }
        elseif($begintime=='' and $endtime!='')
        {
            $sqlw.=' and tm_tohelp.createtime>='.$endtime.' and tm_tohelp.createtime<'.($endtime+24*60*60);
        }



        $page = empty($_REQUEST['page'])?1:$_REQUEST['page'];
        $pagesize =10;





        //$sqlw.=' and gotegary=2';

        $room = M('tohelp');


        $sqlw.=' and tm_tohelp.status>0 and ((select username from tm_user where id=tm_tohelp.userid) like "%'.$keyword.'%" or (select realname from tm_user where id=tm_tohelp.userid) like "%'.$keyword.'%")';


        //$info=$room->field('id,userid,orderid,price,begintime,status,createtime,(select username from tm_user where id=tm_tohelp.userid) as username,(select realname from tm_user where id=tm_tohelp.userid) as realname')->order(" createtime desc")->where($sqlw)->limit(($page-1)*$pagesize,$pagesize)->select();



        $info=$room->field('tm_tohelp.id,tm_tohelp.userid,tm_tohelp.orderid,tm_tohelp.price,tm_tohelp.status,tm_tohelp.createtime,(select username from tm_user where id=tm_tohelp.userid) as username,(select realname from tm_user where id=tm_tohelp.userid) as realname,tm_gethelp.id as id1,tm_gethelp.userid as userid1,tm_gethelp.orderid as orderid1,tm_gethelp.price as price1,tm_gethelp.begintime as begintime1,tm_gethelp.status as status1,tm_gethelp.createtime as createtime1,tm_gethelp.pic,(select username from tm_user where id=tm_gethelp.userid) as username1,(select realname from tm_user where id=tm_gethelp.userid) as realname1,(select createtime from tm_toget where toid=tm_tohelp.id and getid=tm_gethelp.id) as begintime')->join('tm_toget on tm_tohelp.id=tm_toget.toid')->join('tm_gethelp on tm_toget.getid=tm_gethelp.id')->order(" tm_toget.createtime desc")->where($sqlw)->limit(($page-1)*$pagesize,$pagesize)->select();

        $sql = "select count(*) as total from tm_tohelp where ".$sqlw;


        $query = $room->query($sql);

        $pageurl="/admin/index/record?";


        $count = $query[0]['total'];

        $this->assign("room",$info);


        $this->assign("total",$count);

        if($count%$pagesize==0)
            $this->assign("count",$count/$pagesize);
        else
            $this->assign("count",(int)floor($count/$pagesize)+1);

        $this->assign("page",$page);

        $o= 'page='.$page.$search;
        $this->assign("o",$o);


        $search= str_replace('_','&',$search);

        $this->assign("search",$search);




        $this->assign("pageurl",$pageurl);
        $this->assign('pagesize',$pagesize);


        $this->assign('begintime',$begintime);
        $this->assign('endtime',$endtime);


        $this->display();
    }

    //提供帮助列表
    public function record_view()
    {
       $id=I('get.id');
        $sqlw='1=1 ';
        $page = empty($_REQUEST['page'])?1:$_REQUEST['page'];
        $pagesize =10;
        $sqlw.=' and id='.$id;

        $room = M('tohelp');





        $info=$room->field('id,orderid,userid,price,createtime,status,begintime,interest,surplus,replay,(select username from tm_user where id=tm_tohelp.userid) as username')->order(" id desc")->where($sqlw)->limit(($page-1)*$pagesize,$pagesize)->select();



        $sql = "select count(*) as total from tm_tohelp where ".$sqlw;


        $query = $room->query($sql);

        $T=M('toget');
        $U=M('user');
        for($i=0;$i<count($info);$i++)
        {
            $tdata=$T->field('tm_gethelp.id,tm_gethelp.orderid,tm_gethelp.userid,tm_gethelp.price,tm_toget.createtime,tm_toget.status,tm_toget.pic,tm_gethelp.begintime,(select username from tm_user where id=tm_gethelp.userid) as username,(select alipay from tm_user where id=tm_gethelp.userid) as alipay,(select mobile from tm_user where id=tm_gethelp.userid) as mobile,(select bank from tm_user where id=tm_gethelp.userid) as bank,(select bankname from tm_user where id=tm_gethelp.userid) as bankname,(select bankcode from tm_user where id=tm_gethelp.userid) as bankcode,(select realname from tm_user where id=tm_gethelp.userid) as realname,(select tusername from tm_user where id=tm_gethelp.userid) as tusername')->join('tm_gethelp on tm_gethelp.id=tm_toget.getid')->where('tm_toget.toid='.$info[$i]['id'])->select();
            for($j=0;$j<count($tdata);$j++)
            {
                $tdata1=$U->field('username,mobile,realname')->where('username="'.$tdata[$j]['tusername'].'"')->find();
                $tdata[$j]['trealname']=$tdata1['realname'];
                $tdata[$j]['tmobile']=$tdata1['mobile'];
            }



            $info[$i]['sub']=$tdata;
        }








        $pageurl="/user/ldohelp?";


        $count = $query[0]['total'];

        $this->assign("room",$info);


        $this->assign("total",$count);

        if($count%$pagesize==0)
            $this->assign("count",$count/$pagesize);
        else
            $this->assign("count",(int)floor($count/$pagesize)+1);

        $this->assign("page",$page);




        $this->assign("pageurl",$pageurl);
        $this->assign('pagesize',$pagesize);





        $this->display();
    }

    //提供帮助
    public function tpipei()
    {

        $keyword=I('get.k');

        $begintime=I('get.b');
        $endtime=I('get.e');
        $search='';



        if(!empty($begintime))
        {

            $search='_b='.$begintime;
            $begintime=strtotime($begintime);
        }
        if(!empty($endtime))
        {

            $search.='_e='.$endtime;
            $endtime=strtotime($endtime);
        }





        $sqlw='1=1 ';

        if($begintime!='' and $endtime!='')
        {
            $sqlw.=' and createtime>='.$begintime.' and createtime<'.($endtime+24*60*60);
        }
        elseif($begintime!='' and $endtime=='')
        {
            $sqlw.=' and createtime>='.$begintime.' and createtime<'.($begintime+24*60*60);
        }
        elseif($begintime=='' and $endtime!='')
        {
            $sqlw.=' and createtime>='.$endtime.' and createtime<'.($endtime+24*60*60);
        }



        $page = empty($_REQUEST['page'])?1:$_REQUEST['page'];
        $pagesize =10;





        //$sqlw.=' and gotegary=2';

        $room = M('tohelp');


        $sqlw.=' and ((select username from tm_user where id=tm_tohelp.userid) like "%'.$keyword.'%" or (select realname from tm_user where id=tm_tohelp.userid) like "%'.$keyword.'%")';


        $info=$room->field('id,userid,orderid,price,begintime,status,createtime,surplus,(select username from tm_user where id=tm_tohelp.userid) as username,(select realname from tm_user where id=tm_tohelp.userid) as realname,(select count(1) from tm_toget where toid=tm_tohelp.id) as number')->order(" createtime desc")->where($sqlw)->limit(($page-1)*$pagesize,$pagesize)->select();

        $sql = "select count(*) as total from tm_tohelp where ".$sqlw;


        $query = $room->query($sql);





        $pageurl="/admin/index/tpipei?";


        $count = $query[0]['total'];

        $this->assign("room",$info);


        $this->assign("total",$count);

        if($count%$pagesize==0)
            $this->assign("count",$count/$pagesize);
        else
            $this->assign("count",(int)floor($count/$pagesize)+1);

        $this->assign("page",$page);

        $o= 'page='.$page.$search;
        $this->assign("o",$o);


        $search= str_replace('_','&',$search);

        $this->assign("search",$search);




        $this->assign("pageurl",$pageurl);
        $this->assign('pagesize',$pagesize);


        $this->assign('begintime',$begintime);
        $this->assign('endtime',$endtime);


        $this->display();
    }


    //得到帮助
    public function gpipei()
    {

        $keyword=I('get.k');

        $begintime=I('get.b');
        $endtime=I('get.e');
        $search='';



        if(!empty($begintime))
        {

            $search='_b='.$begintime;
            $begintime=strtotime($begintime);
        }
        if(!empty($endtime))
        {

            $search.='_e='.$endtime;
            $endtime=strtotime($endtime);
        }





        $sqlw='1=1 ';

        if($begintime!='' and $endtime!='')
        {
            $sqlw.=' and createtime>='.$begintime.' and createtime<'.($endtime+24*60*60);
        }
        elseif($begintime!='' and $endtime=='')
        {
            $sqlw.=' and createtime>='.$begintime.' and createtime<'.($begintime+24*60*60);
        }
        elseif($begintime=='' and $endtime!='')
        {
            $sqlw.=' and createtime>='.$endtime.' and createtime<'.($endtime+24*60*60);
        }



        $page = empty($_REQUEST['page'])?1:$_REQUEST['page'];
        $pagesize =10;



        $sqlw.=' and ((select username from tm_user where id=tm_gethelp.userid) like "%'.$keyword.'%" or (select realname from tm_user where id=tm_gethelp.userid) like "%'.$keyword.'%")';


        //$sqlw.=' and gotegary=2';

        $room = M('gethelp');





        $info=$room->field('id,userid,orderid,price,begintime,status,createtime,surplus,(select username from tm_user where id=tm_gethelp.userid) as username,(select realname from tm_user where id=tm_gethelp.userid) as realname,(select count(1) from tm_toget where getid=tm_gethelp.id) as number')->order("createtime desc")->where($sqlw)->limit(($page-1)*$pagesize,$pagesize)->select();

        $sql = "select count(*) as total from tm_gethelp where ".$sqlw;


        $query = $room->query($sql);





        $pageurl="/admin/index/gpipei?";


        $count = $query[0]['total'];

        $this->assign("room",$info);


        $this->assign("total",$count);

        if($count%$pagesize==0)
            $this->assign("count",$count/$pagesize);
        else
            $this->assign("count",(int)floor($count/$pagesize)+1);

        $this->assign("page",$page);


        $o= 'page='.$page.$search;
        $this->assign("o",$o);


        $search= str_replace('_','&',$search);

        $this->assign("search",$search);

        $this->assign("pageurl",$pageurl);
        $this->assign('pagesize',$pagesize);


        $this->assign('begintime',$begintime);
        $this->assign('endtime',$endtime);


        $this->display();
    }

    //手动匹配
    public function dpipei()
    {


        $id=I('get.id');
        $t=I('get.t');
        $this->assign('t',$t);
        $k=I('get.k');
        $this->assign('k',$k);

        $o=I('get.o');
        $this->assign('o',$o);

        if($t==1)
        {
            $T=M('tohelp');
        }
        else
        {
            $T=M('gethelp');
        }
        $data=$T->where('id='.$id)->find();
        $this->assign('data',$data);








        $page = empty($_REQUEST['page'])?1:$_REQUEST['page'];
        $pagesize =30;
        //$userid=session('userid');


        $sqlw='1=1 ';
        $sqlw.=' and price<='.$data['price'];

        $sqlw.=' and status=0 and userid!='.$data['userid'];

        


        if($t==1)
        {
            if(!empty($k))
            {
                $sqlw.=' and (orderid like "%'.$k.'%" or (select username from tm_user where tm_gethelp.userid=tm_user.id) like  "%'.$k.'%" or (select realname from tm_user where tm_gethelp.userid=tm_user.id) like  "%'.$k.'%")';
            }

            $room = M('gethelp');
            $info=$room->field('id,userid,orderid,price,begintime,status,createtime,surplus,gotegary,(select username from tm_user where id=tm_gethelp.userid) as username,(select realname from tm_user where id=tm_gethelp.userid) as realname,(select count(1) from tm_toget where getid=tm_gethelp.id) as number')->order(" createtime asc")->where($sqlw)->limit(($page-1)*$pagesize,$pagesize)->select();

            $sql = "select count(*) as total from tm_gethelp where ".$sqlw;
        }
        else
        {
            if(!empty($k))
            {
                $sqlw.=' and (orderid like "%'.$k.'%" or (select username from tm_user where tm_tohelp.userid=tm_user.id) like  "%'.$k.'%" or (select realname from tm_user where tm_tohelp.userid=tm_user.id) like  "%'.$k.'%")';
            }
            $room = M('tohelp');
            $info=$room->field('id,userid,orderid,price,begintime,status,createtime,surplus,(select username from tm_user where id=tm_tohelp.userid) as username,(select realname from tm_user where id=tm_tohelp.userid) as realname,(select count(1) from tm_toget where toid=tm_tohelp.id) as number')->order(" createtime asc")->where($sqlw)->limit(($page-1)*$pagesize,$pagesize)->select();

            $sql = "select count(*) as total from tm_tohelp where ".$sqlw;
        }







        $query = $room->query($sql);




        if(empty($k))
        {
             $pageurl="/admin/index/dpipei.html?id=". $id."&t=".$t."&"; 
        }
        else
        {
               $pageurl="/admin/index/dpipei.html?id=". $id."&t=".$t."&k=".$k."&"; 
        }
      


        $count = $query[0]['total'];

        $this->assign("room",$info);


        $this->assign("total",$count);

        if($count%$pagesize==0)
            $this->assign("count",$count/$pagesize);
        else
            $this->assign("count",(int)floor($count/$pagesize)+1);

        $this->assign("page",$page);




        $this->assign("pageurl",$pageurl);
        $this->assign('pagesize',$pagesize);




        $this->display();
    }

    //处理手动匹配
    public function dodpipei()
    {
        $id=I('post.id');
        $subid=I('post.subid');


        $str='';
        foreach($subid as $vo)
        {
            $str.=$vo.',';
        }

        $str=substr($str,0,strlen($str)-1);



        $t=I('post.t');
        $o=I('post.o');

        $o=str_replace('_','&',$o);


        if($t==1)
        {
            $T=M('tohelp');
            $data=$T->where('id='.$id)->find();
            $G=M('gethelp');
			
			if($data['surplus']==0)
			{
				
				
				$gdata=$G->field('sum(price) as total')->where('id in('.$str.')')->select();
	
				if($gdata[0]['total']>$data['price'])
				{
					echo("<script>alert('匹配金额大于待匹配金额，请重新选择匹配金额！');history.go(-1);</script>");
					die();
				}
                if($gdata[0]['total']<$data['price'])
                {
                    echo("<script>alert('匹配金额小于待匹配金额，请重新选择匹配金额！');history.go(-1);</script>");
                    die();
                }
				$D=M('toget');
				$createtime=time();
				$D->where('toid='.$id)->delete();
	
	
	
				if($gdata[0]['total']==$data['price'])
				{
					$arrt=array('status'=>1,
                        'begintime'=>$createtime);

	
				}
				else
				{
					$arrt=array('surplus'=>$data['price']-$gdata[0]['total'],
                        'begintime'=>$createtime
                        );
				}
				$T->where('id='.$id)->save($arrt);
	
	
				$arrg=array('status'=>1);
	
	
				//发送短信
				//提供帮助
				$udata=$T->field('(select username from tm_user where id=tm_tohelp.userid) as username,(select mobile from tm_user where id=tm_tohelp.userid) as mobile')->where('id='.$id)->find();
				$this->sms($udata['mobile'],$udata['username'],4);
	
	
				foreach($subid as $vo) {
					if (empty($vo)) {
						break;
					}
					$G->where('id=' . $vo)->save($arrg);
					$arr1 = array(
						'toid' => $id,
						'getid' => $vo,
						'createtime' => $createtime,
						'status' => 1,
					);
					$D->add($arr1);
	
					//发送短信
					$gdata=$G->field('(select username from tm_user where id=tm_gethelp.userid) as username,(select mobile from tm_user where id=tm_gethelp.userid) as mobile')->where('id='.$vo)->find();
					$this->sms($gdata['mobile'],$gdata['username'],5);
				}
	
	
	
	
	
	
				
                if($t==1)
                    echo("<script>alert('匹配成功！');location.href='/admin/index/tpipei.html?".$o."';</script>");
                else
                    echo("<script>alert('匹配成功！');location.href='/admin/index/gpipei.html?".$o."';</script>");
                die();
			
		   }
		    else
			{
			    				
				
				$gdata=$G->field('sum(price) as total')->where('id in('.$str.')')->select();
	
				if($gdata[0]['total']>$data['surplus'])
				{
					echo("<script>alert('匹配金额大于待匹配金额，请重新选择匹配金额！');history.go(-1);</script>");
					die();
				}
                if($gdata[0]['total']<$data['surplus'])
                {
                    echo("<script>alert('匹配金额小于待匹配金额，请重新选择匹配金额！');history.go(-1);</script>");
                    die();
                }
				$D=M('toget');
				$createtime=time();
				//$D->where('toid='.$id)->delete();
	
	
	
				if($gdata[0]['total']==$data['surplus'])
				{
					$arrt=array('status'=>1,
					'surplus'=>0,
                        'begintime'=>$createtime
					);
	
				}
				else
				{
					$arrt=array('surplus'=>$data['surplus']-$gdata[0]['total'],
                        'begintime'=>$createtime
                        );
				}
				$T->where('id='.$id)->save($arrt);
	
	
				$arrg=array('status'=>1,
                    'begintime'=>$createtime
                    );
	
	
				//发送短信
				//提供帮助
				$udata=$T->field('(select username from tm_user where id=tm_tohelp.userid) as username,(select mobile from tm_user where id=tm_tohelp.userid) as mobile')->where('id='.$id)->find();
				$this->sms($udata['mobile'],$udata['username'],4);
	
	
				foreach($subid as $vo) {
					if (empty($vo)) {
						break;
					}
					$G->where('id=' . $vo)->save($arrg);
					$arr1 = array(
						'toid' => $id,
						'getid' => $vo,
						'createtime' => $createtime,
						'status' => 1,
					);
					$D->add($arr1);
	
					//发送短信
					$gdata=$G->field('(select username from tm_user where id=tm_gethelp.userid) as username,(select mobile from tm_user where id=tm_gethelp.userid) as mobile')->where('id='.$vo)->find();
					$this->sms($gdata['mobile'],$gdata['username'],5);
				}
	
	
	
	
	
	
				if($t==1)
                    echo("<script>alert('匹配成功！');location.href='/admin/index/tpipei.html?".$o."';</script>");
                else
                    echo("<script>alert('匹配成功！');location.href='/admin/index/gpipei.html?".$o."';</script>");
                die();
				
				
			}


        }
        else
        {


            $T=M('gethelp');
            $data=$T->where('id='.$id)->find();
            $G=M('tohelp');
			
			
			
			if($data['surplus']==0)
			{
				
				
				$gdata=$G->field('sum(price) as total')->where('id in('.$str.')')->select();
				
				
				
				if($gdata[0]['total']>$data['price'])
				{
					echo("<script>alert('匹配金额大于待匹配金额，请重新选择匹配金额！');history.go(-1);</script>");
					die();
				}
                if($gdata[0]['total']<$data['price'])
                {
                    echo("<script>alert('匹配金额小于待匹配金额，请重新选择匹配金额！');history.go(-1);</script>");
                    die();
                }
	
	
				$D=M('toget');
				$createtime=time();
				$D->where('getid='.$id)->delete();
	
	
				if($gdata[0]['total']==$data['price'])
				{
					$arrt=array('status'=>1,
					'surplus'=>0,
                        'begintime'=>$createtime);
	
				}
				else
				{
					$arrt=array('surplus'=>$data['price']-$gdata[0]['total'],'begintime'=>$createtime);
				}
				$T->where('id='.$id)->save($arrt);
	
	
				$arrg=array('status'=>1,'begintime'=>$createtime);
	
	
				//发送短信
				//提供帮助
				//发送短信
				$udata=$T->field('(select username from tm_user where id=tm_gethelp.userid) as username,(select mobile from tm_user where id=tm_gethelp.userid) as mobile')->where('id='.$id)->find();
	
				$this->sms($udata['mobile'],$udata['username'],5);
	
	
				foreach($subid as $vo) {
					if (empty($vo)) {
						break;
					}
					$G->where('id=' . $vo)->save($arrg);
					$arr1 = array(
						'toid' =>  $vo,
						'getid' =>$id,
						'createtime' => $createtime,
						'status' => 1,
					);
					$D->add($arr1);
					//发送短信
					$gdata=$G->field('(select username from tm_user where id=tm_tohelp.userid) as username,(select mobile from tm_user where id=tm_tohelp.userid) as mobile')->where('id='.$vo)->find();
					$this->sms($gdata['mobile'],$gdata['username'],4);
	
	
				}
				
                if($t==1)
                    echo("<script>alert('匹配成功！');location.href='/admin/index/tpipei.html?".$o."';</script>");
                else
                    echo("<script>alert('匹配成功！');location.href='/admin/index/gpipei.html?".$o."';</script>");
				die();
			}
			else
			{
								
				$gdata=$G->field('sum(price) as total')->where('id in('.$str.')')->select();
				if($gdata[0]['total']>$data['surplus'])
				{
					echo("<script>alert('匹配金额大于待匹配金额，请重新选择匹配金额！');history.go(-1);</script>");
					die();
				}

                if($gdata[0]['total']<$data['surplus'])
                {
                    echo("<script>alert('匹配金额大于待匹配金额，请重新选择匹配金额！');history.go(-1);</script>");
                    die();
                }
	
	
				$D=M('toget');
				$createtime=time();
				//$D->where('getid='.$id)->delete();
	
	
				if($gdata[0]['total']==$data['surplus'])
				{
					$arrt=array('status'=>1,
					'surplus'=>0,
                        'begintime'=>$createtime
					);
	
				}
				else
				{
					$arrt=array('surplus'=>$data['surplus']-$gdata[0]['total'],'begintime'=>$createtime);
				}
				$T->where('id='.$id)->save($arrt);
	
	
				$arrg=array('status'=>1,'begintime'=>$createtime);
	
	
				//发送短信
				//提供帮助
				//发送短信
				$udata=$T->field('(select username from tm_user where id=tm_gethelp.userid) as username,(select mobile from tm_user where id=tm_gethelp.userid) as mobile')->where('id='.$id)->find();
	
				$this->sms($udata['mobile'],$udata['username'],5);
	
	
				foreach($subid as $vo) {
					if (empty($vo)) {
						break;
					}
					$G->where('id=' . $vo)->save($arrg);
					$arr1 = array(
						'toid' =>  $vo,
						'getid' =>$id,
						'createtime' => $createtime,
						'status' => 1,
					);
					$D->add($arr1);
					//发送短信
					$gdata=$G->field('(select username from tm_user where id=tm_tohelp.userid) as username,(select mobile from tm_user where id=tm_tohelp.userid) as mobile')->where('id='.$vo)->find();
					$this->sms($gdata['mobile'],$gdata['username'],4);
	
	
				}
				if($t==1)
                    echo("<script>alert('匹配成功！');location.href='/admin/index/tpipei.html?".$o."';</script>");
                else
                    echo("<script>alert('匹配成功！');location.href='/admin/index/gpipei.html?".$o."';</script>");
				die();

				}




        }
    }


    //折单
    public function caidan()
    {
        $id=I('get.id');
        $t=I('get.t');
        $o=I('get.o');
        if($t==1)
        {
            $D=M('tohelp');
            $data=$D->where('id='.$id)->find();

        }
        else
        {
            $D=M('gethelp');
            $data=$D->where('id='.$id)->find();
        }


        $this->assign('room',$data);
        $this->assign('t',$t);
        $this->assign('o',$o);
        $this->display();
    }
    //处理折单
    public function docaidan()
    {
        $t=I('post.t');
        $id=I('post.id');
        $o=I('post.o');
        $price=I('post.price');
        $pricedec=0;
        if($t==1)
        {
            $D=M('tohelp');
            $data=$D->where('id='.$id)->find();


            $pricedec=$data['price']-$price;
            $arr=array('price'=>$price);
            $D->where('id='.$id)->save($arr);
            $arr=array('orderid'=>$data['orderid'],
                'userid'=>$data['userid'],
                'price'=>$pricedec,
                'createtime'=>$data['createtime'],
                'status'=>$data['status'],
                'begintime'=>$data['begintime'],
             );
            $D->add($arr);
        }
        else
        {
            $D=M('gethelp');
            $data=$D->where('id='.$id)->find();
            $pricedec=$data['price']-$price;
            $arr=array('price'=>$price);
            $D->where('id='.$id)->save($arr);
            $arr=array('orderid'=>$data['orderid'],
                'userid'=>$data['userid'],
                'price'=>$pricedec,
                'createtime'=>$data['createtime'],
                'status'=>$data['status'],
                'begintime'=>$data['begintime'],
            );
            $D->add($arr);
        }

        $o=str_replace('_','&',$o);

        if($t==1)
        {
            echo("<script>alert('拆分成功');location.href='/admin/index/tpipei.html?".$o."';</script>");
        }
        else
        {
            echo("<script>alert('拆分成功');location.href='/admin/index/gpipei.html?".$o."';</script>");
        }
        die();

    }
    //提供订单删除
    public function tpipeidel()
    {
        $id=I('get.id');
        $o=I('get.o');
        $D=M('tohelp');
        $D->where('id='.$id)->delete();
        echo("<script>alert('删除成功');location.href='/admin/index/tpipei.html?".$o."';</script>");
        die();
    }

    //提供订单删除
    public function gpipeidel()
    {
        $id=I('get.id');
        $o=I('get.o');
        $o=str_replace('_','&',$o);

        $D=M('gethelp');
        $D->where('id='.$id)->delete();
        echo("<script>alert('删除成功');location.href='/admin/index/gpipei.html?".$o."';</script>");
        die();
    }


    public function news()
    {

        $t=$_REQUEST['t'];

        $search='';

        if(!empty($mobile))
            $search.='&t='.$t;


        $other=empty($_REQUEST['o'])?0:$_REQUEST['o'];
        $page = empty($_REQUEST['page'])?1:$_REQUEST['page'];
        $pagesize =10;

        $sqlw='1=1 and ("'.$t.'"="" or title like "%'.$t.'%")';

        $room = M('news');
        //$info = $room->where("status=0")->limit(($page-1)*$page_size,$page_size)->select();
        //$info = $room->join('left join tm_member on tm_task.memberid = tm_member.memberid ')->limit(($page-1)*$page_size,$page_size)->select();
        //$memberid=session('userid');




        $info=$room->order(" id desc")->where($sqlw)->limit(($page-1)*$pagesize,$pagesize)->select();

        $sql = "select count(*) as total from tm_news where ".$sqlw;


        $query = $room->query($sql);





        $pageurl="/admin/index/news.html?";


        $count = $query[0]['total'];

        $this->assign("room",$info);


        $this->assign("total",$count);

        if($count%$pagesize==0)
            $this->assign("count",$count/$pagesize);
        else
            $this->assign("count",(int)floor($count/$pagesize)+1);

        $this->assign("page",$page);


        $this->assign("search",$search);

        $this->assign("pageurl",$pageurl);
        $this->assign('pagesize',$pagesize);


        $this->assign('t',$t);


        $this->display();

    }
    public function news_view()
    {
        $id=I('get.id');
        if(empty($id))
        {
            echo"<script>alert('没有相应的公告！');history.go(-1);</script>";
            die();
        }
        $D=M('news');
        $room=$D->where('id='.$id)->find();
        $this->assign('room',$room);
        $this->display();

    }


   public function message()
    {

        $t=$_REQUEST['t'];

        $search='';

        if(!empty($mobile))
            $search.='&t='.$t;


        $other=empty($_REQUEST['o'])?0:$_REQUEST['o'];
        $page = empty($_REQUEST['page'])?1:$_REQUEST['page'];
        $pagesize =10;

        $sqlw='1=1 and ("'.$t.'"="" or tm_message.title like "%'.$t.'%")';

        $room = M('message');
        //$info = $room->where("status=0")->limit(($page-1)*$page_size,$page_size)->select();
        //$info = $room->join('left join tm_member on tm_task.memberid = tm_member.memberid ')->limit(($page-1)*$page_size,$page_size)->select();
        //$memberid=session('userid');




        $info=$room->field('tm_message.*,tm_user.username')->join('left join tm_user on tm_user.id=tm_message.userid')->order('tm_message.id desc')->where($sqlw)->limit(($page-1)*$pagesize,$pagesize)->select();

        
        $sql = "select count(*) as total from tm_message where ".$sqlw;


        $query = $room->query($sql);





        $pageurl="/Admin/index/message.html?";


        $count = $query[0]['total'];

        $this->assign("room",$info);


        $this->assign("total",$count);

        if($count%$pagesize==0)
            $this->assign("count",$count/$pagesize);
        else
            $this->assign("count",(int)floor($count/$pagesize)+1);

        $this->assign("page",$page);


        $this->assign("search",$search);

        $this->assign("pageurl",$pageurl);
        $this->assign('pagesize',$pagesize);


        $this->assign('t',$t);


        $this->display();

    }

        public function message_replay()
    {
        $id=I('get.id');
        if(!is_numeric($id))
        {
            echo ("<script>alert('无效参数！');history.go(-1);</script>");
            die();
        }



        $D=M('message');
        $room=$D->field('tm_message.*,tm_user.username')->join('left join tm_user on tm_user.id=tm_message.userid')->where('tm_message.id='.$id)->find();
        if($room['status']==0)
        {
            $D->where('id='.$id)->setinc('status',1);
        }
        $this->assign('room',$room);
        $this->display();
    }
    public function domessage_replay()
    {
        $id=I('post.id');
        $content=I('post.content');
        $D=M('message');
        $createtime=time();
        $arr=array(
             'hfcontent'=>$content,
             'replaytime'=>$createtime,
            );
        $D->where('id='.$id)->save($arr);
        echo 1;
        die();
    }

    public function messagedel()
    {
        $id=I('post.id');
        $D=M('message');
        $D->where('id='.$id)->delete();
        echo 1;
        die();
    }

    public function donews_add()
    {
        $title=I('post.title');
       // $content=htmlspecialchars(I('post.editor'));

        $content=I('post.editor');

        $D=M('news');
        $arr=array('title'=>$title,
            'content'=>$content,
            'createtime'=>time(),
            );
        $D->add($arr);
        echo("<script>alert('发布公告成功');location.href='/admin/index/news.html'</script>");
        die();
    }
    public function delnews()
    {
        $id=I('post.id');
        $D=M('news');
        $D->where('id='.$id)->delete();
        echo 1;
        die();
    }
    public function news_edit()
    {
        $id=I('get.id');
        $D=M('news');
        $data=$D->where('id='.$id)->find();
        $this->assign('room',$data);
        $this->display();
    }
    public function donews_edit()
    {
        $id=I('post.id');
        $title=I('post.title');
       // $content=htmlspecialchars(I('post.editor'));
        $content=I('post.editor');

        $D=M('news');
        $arr=array('title'=>$title,
            'content'=>$content,

        );
        $D->where('id='.$id)->save($arr);
        echo("<script>alert('修改公告成功');location.href='/admin/index/news.html'</script>");
        die();
    }


    public function product_add() {
        $this->display();

    }

    public function doproduct_add() {
    	$data = I('post.');
        $arr=array(
        	'productname'=>$data['productname'],
            'number'=>$data['number'],
            'pic'=>$data['img1url'],
            'pics'=> json_encode(array($data['img1url2'], $data['img1url3'])),
            'price'=>$data['price'],
            'content'=>$data['editor'],
            'createtime'=>time(),
        );
        if(M('product') -> add($arr)) {
        	echo("<script>alert('添加产品成功!');location.href='/admin/index/product.html'</script>");
        	die();
        }
    }


    public function product() {
		//获取网址参数
		$data = I('get.');
        $search='';

        if(!empty($data['k'])) {
            $search='_k='.$data['k'];
        }

        if(!empty($data['b'])) {
            $search='_b='.$data['b'];
            $data['b']=strtotime($data['b']);
        }

        if(!empty($data['e'])) {
            $search.='_e='.$data['e'];
            $data['e']=strtotime($data['e']);
        }

        $sqlw='1=1 ';

        if($data['b']!='' and $data['e']!='') {
            $sqlw.=' and createtime>='.$data['b'].' and createtime<'.($data['e']+24*60*60);
        } elseif($data['b']!='' and $data['e']=='') {
            $sqlw.=' and createtime>='.$data['b'].' and createtime<'.($data['b']+24*60*60);
        } elseif($data['b']=='' and $data['e']!='') {
            $sqlw.=' and createtime>='.$data['e'].' and createtime<'.($data['e']+24*60*60);
        }

        $page = empty($_REQUEST['page'])?1:$_REQUEST['page'];
        $pagesize =10;

        $sqlw.=' and (number like "%'.$data['k'].'%" or productname like "%'.$data['k'].'%")';

        $room = M('product');
        //$info = $room->where("status=0")->limit(($page-1)*$page_size,$page_size)->select();
        //$info = $room->join('left join tm_member on tm_task.memberid = tm_member.memberid ')->limit(($page-1)*$page_size,$page_size)->select();
        //$memberid=session('userid');

        $info=$room->order(" id desc")->where($sqlw)->limit(($page-1)*$pagesize,$pagesize)->select();

        $sql = "select count(*) as total from tm_product where ".$sqlw;

        $query = $room->query($sql);

        $pageurl="/admin/index/product.html?";

        $count = $query[0]['total'];

        $this->assign("room",$info);

        $this->assign("total",$count);

        if($count%$pagesize==0) {
            $this->assign("count",$count/$pagesize);
		} else {
            $this->assign("count",(int)floor($count/$pagesize)+1);
		}

        $this->assign("page",$page);

        $o= 'page='.$page.$search;
        $this->assign("o",$o);

        $search= str_replace('_','&',$search);

        $this->assign("search",$search);

        $this->assign("pageurl",$pageurl);
        $this->assign('pagesize',$pagesize);

        $this->assign('keyword',$data['k']);
        $this->assign('begintime',$data['b']);
        $this->assign('endtime',$data['e']);
        //$this->assign('t',$t);
        $this->display();
    }

    public function product_edit() {
        $id=I('get.id');
        $o=I('get.o');
        $this->assign('o',$o);
        $D=M('product');
        $data=$D->where('id='.$id)->find();
        $this->assign('vo',$data);
        $this -> assign('pics', json_decode($data['pics']));
        $this->display();
    }

    public function doproduct_edit() {
        $data = I('post.');
        $o=str_replace('_','&',$data['o']);

        $arr=array(
        	'productname'=>$data['productname'],
            'number'=>$data['number'],
            'pic'=>$data['img1url'],
            'pics'=> json_encode(array($data['img1url2'], $data['img1url3'])),
            'price'=>$data['price'],
            'content'=>$data['editor']
        );
        if(M('product') -> where('id = ' . $data['id'])->save($arr)) {
        	echo("<script>alert('修改产品成功!');location.href='/admin/index/product.html?".$o."'</script>");
			die();
        }
    }

    public function delproduct() {
        $id = I('post.id');
        $D = M('product');
        $D -> where('id='.$id) -> delete();
        echo 1;
        die();
    }
    
    public function order() {
    	$data = I('get.');

        $search = '';

        $sqlw = ' 1 = 1 ';
        if(!empty($data['b']) && !empty($data['e'])) {
        	$b = strtotime($data['b']);
        	$e = strtotime($data['e']) + 24 * 60 * 60;
        } else if(!empty($data['b']) && $data['e'] == '') {
        	$b =  strtotime($data['b']);
        	$e = $b + 24 * 60 * 60;
        } else if($data['b'] == '' && !empty($data['e'])) {
        	$b = strtotime($data['e']);
        	$e =  $b + 24 * 60 * 60;
        } else {
        	$b = 0;
        	$e = 1999999999;
        }
		$sqlw .= ' AND createtime >= ' . $b . ' AND createtime < ' . $e;

        $data['b'] = date('Y-m-d', $b);
        $data['e'] = date('Y-m-d', $e);

        $this->assign('begintime', $data['b']);
        $this->assign('endtime', $data['e']);

		$search .= '&b=' . $data['b'];
		$search .= '&e=' . $data['e'];

		if(!empty($data['status'])) {
			$search .= '&status=' . $data['status'];
			$sqlw .= ' AND status = ' . $data['status'];
			$this ->assign('status', $data['status']);
		}
		
		$other = empty($_REQUEST['o'])?0:$_REQUEST['o'];
        $page = empty($_REQUEST['page'])?1:$_REQUEST['page'];
        $pagesize = 10;

		$room = M('order');

    	$info = $room
    	-> field('tm_order.id,tm_order.userid,tm_order.orderid,tm_order.productname,
    	tm_order.price, tm_order.number,tm_order.createtime,tm_order.status,
		tm_order_address.contact,tm_order_address.kdgs,tm_order_address.kddh')
    	-> join('tm_order_address on tm_order.id=tm_order_address.orderid')
    	-> order('tm_order.status ASC') -> where($sqlw)
    	-> limit(($page-1)*$pagesize,$pagesize) -> select();

        $query = $room->query('select count(*) as total from tm_order where ' . $sqlw);

        $pageurl='/admin/index/order.html?';

        $count = $query[0]['total'];

        $this -> assign('room',$info);

        $this -> assign('total',$count);

        if($count%$pagesize==0){
        	$this->assign('count',$count/$pagesize);
        } else {
        	$this->assign('count',(int)floor($count/$pagesize)+1);
        }

        $this->assign('page', $page);

        $this->assign('search', $search);

        $this->assign('pageurl', $pageurl);
        $this->assign('pagesize', $pagesize);
        $this->display();
    }
    
    public function order_detail() {
        $id=I('get.id');

        $this->assign('data', M('order')->where('id='.$id)->find());
        $this->assign('pdata', M('order_product')->where('orderid='.$id)->select());
        $this->assign('adata', M('order_address')->where('orderid='.$id)->find());

        $this->display();
    }

    public function order_dispatched() {
    	$id=I('get.id');
    	if(IS_POST) {
    		$data = I('post.');
    		if(M('order_address') -> where('orderid = ' . $id) -> save(array(
    			'kdgs' => $data['kdgs'],
    			'kddh' => $data['kddh'],
    			'fhtime' => time(),
    		))) {
    			if(M('order') -> where('id = ' . $id) -> save(array('status' => 3)) > 0) {
    				echo '<script>alert("发货成功！");location.href="/admin/index/order.html";</script>';
        			die();
    			} else {
    				echo"<script>alert('发货失败！');history.go(-1);</script>";
        			die();
    			}
    		}
    	}

		$this->assign('data', M('order')->where('id='.$id)->find());
		$this->assign('adata', M('order_address')->where('orderid='.$id)->find());

    	$this -> display();
    }

    //完善资料
    public function profile() {

        $userid=I('get.id');
        $D=M('user');
        $data=$D->where('id='.$userid)->find();

        $this->assign('room',$data);
        $this->display();

    }

    public function doprofile()
    {
        $mobile=I('post.mobile');
        $email=I('post.email');
        $tel=I('post.tel');
        $address=I('post.address');
        $sex=I('post.sex');
        $real=I('real');
        $id=session('userid');
        $arr=array(
            'mobile'=>$mobile,
            'email'=>$email,
            'tel'=>$tel,
            'address'=>$address,
            'sex'=>$sex,
            'realname'=>$real,
        );
        $D=M('user');
        $D->where('id='.$id)->save($arr);
        echo"<script>alert('修改资料成功！');history.go(-1);</script>";
        die();


    }
    //ajax提交数据
    public function ajax()
    {
        $action=I('post.action');
        switch($action) {

            case 'checkinfo':

                $userid=I('id');
                $mobile=I('post.mobile');
                $weixin=I('post.weixin');
                $alipay=I('post.alipay');
                $address=I('post.address');
                $arr=array(
                    'mobile'=>$mobile,
                    'weixin'=>$weixin,
                    'alipay'=>$alipay,
                    'address'=>$address,
                );
                $D=M('user');
                $data=$D->field('mobile')->where('mobile='.$mobile.' and id!='.$userid)->find();
                if(!empty($data))
                {
                    echo 2;
                    die();
                }


                $D->where('id='.$userid)->save($arr);
                echo 1;
                die();
                break;
            case 'checkpaper':

                $userid=I('id');
                $realname=I('post.realname');
                $paper=I('post.paper');

                $arr=array(
                    'realname'=>$realname,
                    'paper'=>$paper,

                );
                $D=M('user');
                $D->where('id='.$userid)->save($arr);
                echo 1;
                die();
                break;
            case 'checkbank'://修改开户行

                $userid=I('post.id');
                $bankname=I('post.bankname');
                $bank=I('post.bank');
                $bankcode=I('post.bankcode');

                $arr=array(
                    'bankname'=>$bankname,
                    'bank'=>$bank,
                    'bankcode'=>$bankcode,

                );
                $D=M('user');
                $D->where('id='.$userid)->save($arr);
                echo 1;
                die();
                break;
            case 'admindelete'://修改开户行

                $userid=I('post.id');
                $D=M('admin');
                $D->where('id='.$userid)->delete();
                echo 1;
                die();
                break;
            case 'password'://修改登录密码

                $userid=I('post.id');

                $newpassword=md5(I('post.newpassword'));
                $D=M('user');

                $arr=array(
                    'password'=>$this->md5pw($newpassword),

                );

                $D->where('id='.$userid)->save($arr);
                echo 1;
                die();
                break;
            case 'paypassword'://修改安全密码

                $userid=I('post.id');

                $newpaypassword=md5(I('post.newpaypassword'));
                $D=M('user');

                $arr=array(
                    'paypassword'=>$this->md5pw($newpaypassword),

                );

                $D->where('id='.$userid)->save($arr);
                echo 1;
                die();
                break;


        }
    }
    //会员登录
    public function dologin()
    {
        $id=I('get.id');
        $D=M('user');
        $data=$D->where('id='.$id)->find();
        if(empty($data))
        {
            echo("<script>alert('无效参数！');history.go(-1);</script>");
            die();
        }
        else
        {
            session('admin_login',1);//0前台登录，1后台登录
            session('userid',$data['id']);
            session('username',$data['username']);
            session('status',$data['status']);//0  未开通，１已开通　２休眠　３冻结　
            session('limit',$data['limit']);//投资额度
            session('logintime',time());//登录时间
            echo("<script>location.href='/user/';</script>");

            die();
        }
    }


    //冻结记录
    public function freeze()
    {


        $keyword=trim(I('get.k'));

        $begintime=I('get.b');
        $endtime=I('get.e');
        $search='';
        
        if(!empty($keyword))
        {
            $search='&k='.$keyword;
        }

   

        if(!empty($begintime))
        {

            $search='_b='.$begintime;
            $begintime=strtotime($begintime);
        }
        if(!empty($endtime))
        {

            $search.='_e='.$endtime;
            $endtime=strtotime($endtime);
        }





        $sqlw='1=1 and status=0  ';

        if($begintime!='' and $endtime!='')
        {
            $sqlw.=' and begintime>='.$begintime.' and begintime<'.($endtime+24*60*60);
        }
        elseif($begintime!='' and $endtime=='')
        {
            $sqlw.=' and begintime>='.$begintime.' and begintime<'.($begintime+24*60*60);
        }
        elseif($begintime=='' and $endtime!='')
        {
            $sqlw.=' and begintime>='.$endtime.' and begintime<'.($endtime+24*60*60);
        }



        $page = empty($_REQUEST['page'])?1:$_REQUEST['page'];
        $pagesize =10;



      $sqlw.=' and ((select username from tm_user where id=tm_freeze.userid) like "%'.$keyword.'%")';




        $room = M('freeze');





        $info=$room->field('id,userid,toid,integral1,integral2,gotegary,title,status,begintime,(select username from tm_user where id=tm_freeze.fromuserid) as fromusername,(select username from tm_user where id=tm_freeze.userid) as username')->order(" id desc")->where($sqlw)->limit(($page-1)*$pagesize,$pagesize)->select();

        $sql = "select count(*) as total from tm_freeze where ".$sqlw;


        $query = $room->query($sql);
        $sql = "select sum(integral1+integral2) as total from tm_freeze where ".$sqlw;

        $integraltotal=$room->query($sql);

        $this->assign('integraltotal',$integraltotal[0]['total']);

        $pageurl="/admin/index/freeze?";


        $count = $query[0]['total'];

        $this->assign("room",$info);


        $this->assign("total",$count);

        if($count%$pagesize==0)
            $this->assign("count",$count/$pagesize);
        else
            $this->assign("count",(int)floor($count/$pagesize)+1);

        $this->assign("page",$page);

        $o= 'page='.$page.$search;
        $this->assign("o",$o);


        $search= str_replace('_','&',$search);

        $this->assign("search",$search);


        $this->assign("search",$search);

        $this->assign("pageurl",$pageurl);
        $this->assign('pagesize',$pagesize);


        $this->assign('begintime',$begintime);
        $this->assign('endtime',$endtime);
        $this->assign('keyword',$keyword);


        $this->display();
    }

    //解冻奖金
    public function dofreeze()
    {
        $id=I('get.id');
        $o=I('get.o');
        $o=str_replace('_','&',$o);

        $D=M('freeze');
        $begintime=time();
        $endtime=15*24*3600;
        $data=$D->where('id='.$id)->find();


        if(!empty($data))
        {

                //解冻奖金，奖金到帐
                $this->freezein($data);
                //已经解冻,改变奖态
                $D->where('id='.$data['id'])->setinc('status',1);

        }
        echo "<script>alert('解冻成功！');location.href='/admin/index/freeze.html?".$o."'</script>";
        die();


    }

    //写入收入记录
    public function freezein($data)
    {



        $I=M('integral');


        //写入收支记录
        //支付记录
        //$userid 会员ID
        //$title 内容
        //$createtime 创建时间
        //$price 消费价格
        //$status 消费状态 1 收入，0 支出
        //$gotegary 1  静态帐户，2动态帐户，3 排单币，4激活币 5  消费帐户
        $begintime=time();
        $status=1;
        if($data['gotegary']==1)
        {
            $gotegary=1;
            $I->where('userid='.$data['userid'])->setinc('integral1',$data['integral1']);
            $I->where('userid='.$data['userid'])->setinc('integral5',$data['integral2']);
        }
        else
        {
            $gotegary=2;
            $I->where('userid='.$data['userid'])->setinc('integral2',$data['integral1']);
            $I->where('userid='.$data['userid'])->setinc('integral5',$data['integral2']);
        }
        $this->Pay_history($data['userid'],$data['title'],$data['integral1'],$begintime,$status,$gotegary);
        $gotegary=5;
        $this->Pay_history($data['userid'],$data['title'],$data['integral2'],$begintime,$status,$gotegary);

        $U=M('user');
        $user=$U->field('username,mobile')->where('id='.$data['userid'])->find();
        //解冻短信
        $this->sms($user['mobile'],$user['username'],3);

    }
    //发送短信
    //$mobile 手机号码，
    //$username 用户名
    //$gotegary 1 提供帮助，２得到帮助
    //发送短信
    public function sms($mobile, $username, $gotegary) {
    	$contentParam = array(
            'name'      => $username
        );
        
        winicSMS($mobile, $contentParam, $gotegary);
    }
    /*
    public function sms($mobile,$username,$gotegary)
    {
        vendor('smsapi');

        //用户账号
        $uid = 'qqq123456';
        //MD5密码
        $pwd = 'aa123456';

        $api = new \SmsApi($uid,$pwd);


        $contentParam = array(

            'name'	=> $username
        );

//变量模板ID

         switch($gotegary)
        {
            case 1:
                $template = '402818';//提供帮助
                break;
            case 2:
                $template = '402819';//得到帮助
                break;
            case 3:
                $template = '402822';//解冻
                break;
            case 4:
                $template = '402821';//提供帮助匹配成功
                break;
            case 5:
                $template = '402820';//得到帮助匹配成功
                break;
             case 6:
                $template = '402853';//审核排单
                break;
        }


//发送变量模板短信
        $result = $api->send($mobile,$contentParam,$template);

    }
	*/

     //删除会员
    public function delete()
    {
        $id=I('post.id');
        $U=M('user');
        $I=M('integral');
        $I->where('userid='.$id)->delete();
        $U->where('id='.$id)->delete();
        echo 1;
        die();
    }
    //删除记录
    public function deletehistory()
    {
        $id=I('post.id');
        $U=M('buy_history');
        $U->where('id='.$id)->delete();
        echo 1;
        die();
    }

    //清除缓存
    public function deldir() {

        header("Content-type: text/html; charset=utf-8");
        //清文件缓存
        $dirs = array('App/Runtime/');
        @mkdir('Runtime',0777,true);
        //清理缓存
        foreach($dirs as $value) {
            $this->rmdirr($value);
        }
        $success= '系统缓存清除成功！';
        $this->assign('success',$success);
        $this->display();
        
    }


    //处理方法
    public function rmdirr($dirname) {
        if (!file_exists($dirname)) {
            return false;
        }
        if (is_file($dirname) || is_link($dirname)) {
            return @unlink($dirname);
        }
        $dir = dir($dirname);
        if($dir){
            while (false !== $entry = $dir->read()) {
                if ($entry == '.' || $entry == '..') {
                    continue;
                }
                //递归
                $this->rmdirr($dirname . DIRECTORY_SEPARATOR . $entry);
            }
        }
        $dir->close();
        return rmdir($dirname);
    }


    /**
     * 密码加密
     */
    function md5pw($passwd)
    {
        $ph = new \Think\PasswordHash();
        return $ph->HashPassword($passwd);

    }

    /**
     * 检测密码是否正确
     * @param $getPwd
     * @param $dataPasswd
     */
    function _checkPwd($getPwd,$dataPasswd)
    {
        $ph = new \Think\PasswordHash();
        if($ph->CheckPassword($getPwd,$dataPasswd))
        {
            return true;
        }
        else
        {
            return false;
        }
    }



    //退出
    public function logout()
    {
        session(null);
        echo"<script>location.href='/admin.php'</script>";
        die();

    }

}